package com.msb.mallcart.Controller;

import com.msb.mallcart.Service.ICartService;
import com.msb.mallcart.vo.Cart;
import com.msb.mallcart.vo.CartItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Auther: xixifeng
 * @Date: 2022/11/10 - 11 - 10 - 17:54
 * @Description: com.msb.mallcart.Controller
 * @version: 1.0
 */

@Controller
public class CartController {


    @Autowired
    ICartService iCartService;

    /**
     * 加入购物车
     * @param skuId
     * @param num
     * @return
     */
    @GetMapping("/addCart")
    public String addCart(@RequestParam("skuId") Long skuId
                            , @RequestParam("num") Integer num
                            , Model model){
        CartItem cartItem=iCartService.addCart(skuId,num);

        model.addAttribute("item", cartItem);
        return "success";
    }

    /**
     * 查询出当前登录用户的所有的购物车信息 响应给前端
     * @param model
     * @return
     */
    @GetMapping("/cart_list")
    public String queryCartList(Model model){

        Cart cart=iCartService.getCartList();
        model.addAttribute("cart",cart);

        return "cartList";
    }

    //查询购物车中选中的商品信息
    @GetMapping(value = "/getUserCartItems")
    @ResponseBody
    public List<CartItem> getUserCartItems(){

        return iCartService.getUserCartItems();
    }


}
